package com.ego.cart.controller;

import com.ego.cart.service.CartService;
import com.ego.commons.pojo.CartPojo;
import com.ego.commons.pojo.EgoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {


    @Autowired
    private CartService cartService;

    //添加商品到购物车
    @RequestMapping("/cart/add/{id}.html")
    public String cartAdd(@PathVariable Long id , int num){
        cartService.cartAdd(id,num);
        return "cartSuccess";
    }

    //购物车列表
    @RequestMapping("/cart/cart.html")
    public String cartList(Model model){

        List<CartPojo> cartPojoList = cartService.cartList();

        model.addAttribute("cartList",cartPojoList);

        return "cart";
    }

    //删除
    @RequestMapping("/cart/delete/{id}.action")
    @ResponseBody
    public EgoResult cartList(@PathVariable Long id){

        return cartService.deleteCartItemByItemId(id);
    }

    //修改
    @RequestMapping({"/service/cart/update/num/{id}/{num}","/cart/update/num/{id}/{num}.action"})
    @ResponseBody
    public EgoResult updateCartItemNum(@PathVariable Long id,@PathVariable int num){



        return cartService.updateCartItemNum(id,num);
    }

    //结算页面
    @RequestMapping("/cart/order-cart.html")
    public String cartOrderCart(@RequestParam List<Long> id,Model model){
        System.out.println("id = " + id);

        List<CartPojo> cartPojos = cartService.orderCartByIds(id);

        model.addAttribute("cartList",cartPojos);

        return "order-cart";
    }


}
